package com.lc.demo.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lc.demo.bean.*;
import com.lc.demo.result.ResponseMessage;
import com.lc.demo.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: sakila
 * @Date: 2022/04/03/10:36
 * @Description:
 */
@RestController
@CrossOrigin
@RequestMapping("/jwc")
public class JwcController {


    @Autowired
    private JwcService jwcService;

    @Autowired
    AdminService adminService;

    @Autowired
    StudentService studentService;

    @Autowired
    ClassGradleService classGradleService;

    @PostMapping(value = "/login")
    public ResponseMessage login(@RequestBody Jwc jwc) {
        Jwc adm = jwcService.login(jwc.getUsername(), jwc.getPassword());
        if (adm != null) {
            return ResponseMessage.successWithMessage(adm.getUsername());
        } else {
            return ResponseMessage.errorWithMessage("用户名或密码错误");
        }
    }

    //院系增删改查
    @PostMapping(value = "/addDepartment")
    public ResponseMessage addDepartment(@RequestBody Department department) {
        jwcService.addDepartment(department);
        return ResponseMessage.success();
    }

    @PostMapping(value = "/deleteDepartment")
    public ResponseMessage deleteDepartment(@RequestBody Department department) {
        jwcService.deleteDepartment(department);
        return ResponseMessage.success();
    }

    @PostMapping(value = "/updateDepartment")
    public ResponseMessage updateDepartment(@RequestBody Department department) {
        jwcService.updateDepartment(department);
        return ResponseMessage.success();
    }

    @PostMapping(value = "/getDepartmentList")
    public ResponseMessage getDepartmentList(@RequestBody Department department) {
        int pageNum = 1;
        int pageSize = 10;
        if (department.getPageNum() != null) {
            pageNum = department.getPageNum();
        }
        if (department.getPageSize() != null) {
            pageSize = department.getPageSize();
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Department> departments=jwcService.getDepartmentList(department);
        PageInfo<Department> page = new PageInfo<>(departments);
        return ResponseMessage.successWithData(page);
    }


    //教务员增删改查
    @PostMapping(value = "/addAdmin")
    public ResponseMessage addAdmin(@RequestBody Admin admin) {
        jwcService.addAdmin(admin);
        return ResponseMessage.success();
    }

    @PostMapping(value = "/deleteAdmin")
    public ResponseMessage deleteAdmin(@RequestBody Admin admin) {
        jwcService.deleteAdmin(admin);
        return ResponseMessage.success();
    }

    @PostMapping(value = "/updateAdmin")
    public ResponseMessage updateAdmin(@RequestBody Admin admin) {
        jwcService.updateAdmin(admin);
        return ResponseMessage.success();
    }

    @PostMapping(value = "/getAdminList")
    public ResponseMessage getAdminList(@RequestBody Admin admin) {
        int pageNum = 1;
        int pageSize = 20;
        if (admin.getPageNum() != null) {
            pageNum = admin.getPageNum();
        }
        if (admin.getPageSize() != null) {
            pageSize = admin.getPageSize();
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Admin> adminList=jwcService.getAdminList(admin);
        PageInfo<Admin> page = new PageInfo<>(adminList);
        return ResponseMessage.successWithData(page);
    }





    //学生信息增删改查
    @PostMapping(value = "/addStudent")
    public ResponseMessage addStudent(@RequestBody Student student) {
        studentService.addStudent(student);
        return ResponseMessage.success();
    }


    @PostMapping(value = "/updateStudent")
    public ResponseMessage updateStudent(@RequestBody Student student) {
        studentService.updateStudent(student);
        return ResponseMessage.success();
    }


    @PostMapping(value = "/findStudent")
    public ResponseMessage findStudent(@RequestBody Student student) {
        Student student1 = studentService.selectByUsername(student.getUsername());
        return ResponseMessage.successWithData(student1);
    }
    
    @PostMapping(value = "/getStudentList")
    public ResponseMessage getStudentList(@RequestBody Student student) {
        int pageNum = 1;
        int pageSize = 10;
        if (student.getPageNum() != null) {
            pageNum = student.getPageNum();
        }
        if (student.getPageSize() != null) {
            pageSize = student.getPageSize();
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Student> studentList=studentService.getStudentList();
        PageInfo<Student> page = new PageInfo<>(studentList);
        return ResponseMessage.successWithData(page);
    }


    @PostMapping(value = "/delStudent")
    public ResponseMessage delStudent(@RequestBody Student student) {
        studentService.delStudentByUsername(student.getUsername());
        return ResponseMessage.success();
    }


    //教师信息增删改查
    @PostMapping(value = "/addTeacher")
    public ResponseMessage addTeacher(@RequestBody Teacher teacher) {
        adminService.addTeacher(teacher);
        return  ResponseMessage.success();
    }

    //教师信息增删改查
    @PostMapping(value = "/delTeacher")
    public ResponseMessage delTeacher(@RequestBody Teacher teacher) {
        adminService.delTeacher(teacher);
        return  ResponseMessage.success();
    }

    //修改教师
    @PostMapping(value = "/updateTeacher")
    public ResponseMessage updateTeacher(@RequestBody Teacher teacher) {
        adminService.updateTeacher(teacher);
        return  ResponseMessage.success();
    }


    @PostMapping(value = "/getTeacherList")
    public ResponseMessage getTeacherList(@RequestBody Teacher teacher) {
        int pageNum = 1;
        int pageSize = 10;
        if (teacher.getPageNum() != null) {
            pageNum = teacher.getPageNum();
        }
        if (teacher.getPageSize() != null) {
            pageSize = teacher.getPageSize();
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Teacher> teacherList=jwcService.getTeacherList(teacher);
        PageInfo<Teacher> page = new PageInfo<>(teacherList);
        return ResponseMessage.successWithData(page);
    }



    @Autowired
    private ScoreService scoreService;





    //添加班级
    @PostMapping(value = "/addClass")
    public ResponseMessage addClass(@RequestBody Classgrade classgrade) {
        classGradleService.addClass(classgrade);
        return ResponseMessage.success();
    }
    //修改班级
    @PostMapping(value = "/updateClass")
    public ResponseMessage updateClass(@RequestBody Classgrade classgrade) {
        classGradleService.updateClass(classgrade);
        return ResponseMessage.success();
    }

    //查询班级列表
    @PostMapping(value = "/getClassList")
    public ResponseMessage getClassList(@RequestBody Classgrade classgrade) {
        // String username = (String) session.getAttribute("loginUser");

        Integer pageNum = classgrade.getPageNum();
        Integer pageSize = classgrade.getPageSize();

        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Classgrade> classgradeList = classGradleService.getClassList();
        PageInfo<Classgrade> page = new PageInfo<>(classgradeList);

        return ResponseMessage.successWithData(page);
    }

    //查询所有成绩
    @PostMapping(value = "/getScoreList")
    public ResponseMessage findScoreList(@RequestBody  Score score) {
        int pageNum = 1;
        int pageSize = 10;
        if (score.getPageNum() != null) {
            pageNum = score.getPageNum();
        }
        if (score.getPageSize() != null) {
            pageSize = score.getPageSize();
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Score> scoreList=scoreService.findScoreList(score);
        PageInfo<Score> page = new PageInfo<>(scoreList);
        return ResponseMessage.successWithData(page);
    }



    @PostMapping(value = "/jwcReview")
    public ResponseMessage review(@RequestBody  Score score) {
        //审核通过 2  审核不通过状态1
        scoreService.adminReview(score);
        return ResponseMessage.success();
    }




}
